package com.sands.network.helper;


import com.sands.network.util.I;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by kunqi.lai on 2016/8/16 14:52.
 * email :781548819@qq.com
 */
public class TokenInterceptor implements Interceptor {
    private static final Charset UTF8 = Charset.forName("UTF-8");//http://120.76.162.194/a
    private static final String URL_BASE = I.Root_server + "?access_token=";
    private static final String URL_PARA = "&json=";
    private static final String TAG = "main";

    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();
        // try the request
        Response originalResponse = chain.proceed(request);
        /**通过如下的办法曲线取到请求完成的数据
         *
         * 原本想通过  originalResponse.body().string()
         * 去取到请求完成的数据,但是一直报错,不知道是okhttp的bug还是操作不当
         *
         * 然后去看了okhttp的源码,找到了这个曲线方法,取到请求完成的数据后,根据特定的判断条件去判断token过期
         */
        int code = originalResponse.code();
        /***************************************/
        if (code == 401) {//根据和服务端的约定判断token过期
            return originalResponse;

        }
        return originalResponse;
    }
}
